package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CartDaoImpl implements CartDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    private int result = 0;

    @Override
    public List<Cart> listCart(Cart cart) throws SQLException{
        List<Cart> list = new ArrayList<>();
        StringBuffer sql  = new StringBuffer();
        sql.append("select * from cart,business,food where cart.businessId=business.businessId and cart.foodId=food.foodId and cart.userId=? ");
        if (cart.getBusinessId()!=null){
            sql.append("and cart.businessId="+cart.getBusinessId());
        }

        try {
            con=DBUtil.getConnection();
            pst = con.prepareStatement(String.valueOf(sql));
            pst.setString(1,cart.getUserId());
            rs = pst.executeQuery();
            while (rs.next()){
                Cart cart1 = new Cart();
                Food food = new Food();
                Business business = new Business();
                food.setFoodId(rs.getInt("foodId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setBusinessId(rs.getInt("businessId"));
                food.setRemarks(rs.getString("remarks"));

                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));

                cart1.setCartId(rs.getInt("cartId"));
                cart1.setFoodId(rs.getInt("foodId"));
                cart1.setBusinessId(rs.getInt("businessId"));
                cart1.setUserId(rs.getString("userId"));
                cart1.setQuantity(rs.getInt("quantity"));
                cart1.setBusiness(business);
                cart1.setFood(food);
                list.add(cart1);
            }
        } finally {
            DBUtil.close(pst);
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) throws SQLException{
        String sql = "insert into cart(foodId,businessId,userId,quantity) values(?,?,?,1)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,foodId);
            pst.setInt(2,businessId);
            pst.setString(3,userId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws SQLException{
        String sql = "update cart set quantity=? where userId=? and businessId=? and foodId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1,quantity);
            pst.setString(2,userId);
            pst.setInt(3,businessId);
            pst.setInt(4,foodId);
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public int removeCart(Cart cart) throws SQLException{
        StringBuffer sql = new StringBuffer();
        sql.append("delete from cart where userId=? and businessId=? ");
        if (cart.getFoodId()!=null){
            sql.append("and foodId="+cart.getFoodId());
    }
        try {
            con=DBUtil.getConnection();
            pst = con.prepareStatement(String.valueOf(sql));
            pst.setString(1,cart.getUserId());
            pst.setInt(2,cart.getBusinessId());
            result = pst.executeUpdate();
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }
}
